import { defineStore } from "pinia";
import { store } from "@/stores";
export const useListStore = defineStore("list", () => {
  const queryParams = reactive<Record<string, any>>({
    curPage: 1,
    pageSize: 10
  });
  function setQueryParams(params: Record<string, any>) {
    Object.assign(queryParams, { ...params });
  }
  function resetQueryParams(keysToDelete?: string[]) {
    if (!keysToDelete || keysToDelete.length === 0) {
      // 如果未提供 keysToDelete 或为空数组，删除所有属性
      Object.keys(queryParams).forEach((key) => {
        delete queryParams[key];
      });
    } else {
      // 如果提供了 keysToDelete，只保留指定的属性，删除其他属性
      const newQueryParams: Record<string, any> = {};
      for (const key of keysToDelete) {
        if (Object.prototype.hasOwnProperty.call(queryParams, key)) {
          newQueryParams[key] = queryParams[key];
        }
      }

      // 清空 queryParams
      Object.keys(queryParams).forEach((key) => {
        delete queryParams[key];
      });

      // 将 newQueryParams 的属性赋值给 queryParams
      Object.assign(queryParams, newQueryParams);
    }
    console.log(queryParams);
  }
  return {
    setQueryParams,
    resetQueryParams,
    queryParams
  };
});

export function useListStoreHook() {
  return useListStore(store);
}
